package com.lcq.gulimall.product.service.impl;

import com.lcq.common.constant.ProductConstant;
import com.lcq.gulimall.product.dao.AttrAttrgroupRelationDao;
import com.lcq.gulimall.product.dao.AttrDao;
import com.lcq.gulimall.product.entity.AttrAttrgroupRelationEntity;
import com.lcq.gulimall.product.entity.AttrEntity;
import com.lcq.gulimall.product.entity.ProductAttrValueEntity;
import com.lcq.gulimall.product.service.AttrAttrgroupRelationService;
import com.lcq.gulimall.product.service.AttrService;
import com.lcq.gulimall.product.service.ProductAttrValueService;
import com.lcq.gulimall.product.vo.*;
import com.mysql.cj.util.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lcq.common.utils.PageUtils;
import com.lcq.common.utils.Query;

import com.lcq.gulimall.product.dao.AttrGroupDao;
import com.lcq.gulimall.product.entity.AttrGroupEntity;
import com.lcq.gulimall.product.service.AttrGroupService;
import org.springframework.transaction.annotation.Transactional;


@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements AttrGroupService {
    @Autowired
    AttrAttrgroupRelationService attrAttrgroupRelationService;

    @Autowired
    AttrAttrgroupRelationDao attrAttrgroupRelationDao;

    @Autowired
    AttrService attrService;

    @Autowired
    AttrDao attrDao;

    @Autowired
    ProductAttrValueService productAttrValueService;

    @Autowired
    AttrGroupService attrGroupService;

    @Autowired
    AttrGroupDao attrGroupDao;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrGroupEntity> page = this.page(
                new Query<AttrGroupEntity>().getPage(params),
                new QueryWrapper<AttrGroupEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params, Integer catId) {
        String key = (String) params.get("key");
        QueryWrapper<AttrGroupEntity> wrapper = new QueryWrapper<>();
//        .eq("catlogId",catId)
//                .and((obj)->{
//                    if(!StringUtils.isNullOrEmpty(key)){
//                        obj.eq("attrGroupId",key).or().like("attrGroupName",key).or().like("descript",key);
//                    }
//                });

        if(catId!=0){
            wrapper.eq("catelog_id",catId);
        }

        if(!StringUtils.isNullOrEmpty(key)){
            wrapper.and((obj)->{
                obj.eq("attr_group_id",key).or().like("attr_group_name",key).or().like("descript",key);
            });
        }

        IPage<AttrGroupEntity> page = this.page(new Query<AttrGroupEntity>().getPage(params),wrapper);
        return new PageUtils(page);
    }

    @Override
    public List<AttrRespVo> queryAttrRelationList(Long gourpId) {
        return attrAttrgroupRelationService.queryAttrRelationList(gourpId);
    }

    @Override
    @Transactional
    public PageUtils queryNoAttrRelationPage(Map<String, Object> params, Long groupId) {
        /**
         * 1、属性不属于任何分组下
         * 2、属性属于groupId所在的catelogId之下
         * */
        AttrGroupEntity attrGroupEntity = this.getBaseMapper().selectById(groupId);
        if (attrGroupEntity == null) {
            return null;
        }
        //有问题
        Long catelogId = attrGroupEntity.getCatelogId();
        List<AttrAttrgroupRelationEntity> list = attrAttrgroupRelationDao.selectList(null);
        if(list==null||list.size()==0){
            return null;
        }

        List<Long> noIds = list.stream().map((entity) -> {
            return entity.getAttrId();
        }).collect(Collectors.toList());

        IPage page = attrService.page(new Query<AttrEntity>().getPage(params),
                new QueryWrapper<AttrEntity>().eq("catelog_id", catelogId).and((wrapper) -> {
                    wrapper.notIn("attr_id", noIds).and((wrap)->{
                        wrap.notIn("attr_type", ProductConstant.AttrEnum.ATTR_SALE_TYPE);
                    });
                }));
        return new PageUtils(page);
    }

    @Override
    public void attrRelation(AttrGroupRelationVo[] attrGroupRelationVos) {
        if(attrGroupRelationVos==null||attrGroupRelationVos.length==0){
            return;
        }
        List<AttrAttrgroupRelationEntity> list = Arrays.stream(attrGroupRelationVos).map((vo)->{
            AttrAttrgroupRelationEntity entity = new AttrAttrgroupRelationEntity();
            BeanUtils.copyProperties(vo,entity);
            return entity;
        }).collect(Collectors.toList());

        attrAttrgroupRelationService.saveBatch(list);
    }

    @Override
    public List<AttrGroupVo> getGroupWithAttr(Long catelogId) {
        if(catelogId==null||catelogId==0){
            return null;
        }
        List<AttrGroupEntity> attrGroupList =  this.getBaseMapper().selectList(new QueryWrapper<AttrGroupEntity>().eq("catelog_id",catelogId));
        if(attrGroupList==null||attrGroupList.size()==0){
            return null;
        }

        return attrGroupList.stream().map((entity)->{
            AttrGroupVo attrGroupVo = new AttrGroupVo();
            BeanUtils.copyProperties(entity,attrGroupVo);
            List<AttrAttrgroupRelationEntity> relationList = attrAttrgroupRelationDao.selectList(
                    new QueryWrapper<AttrAttrgroupRelationEntity>()
                            .eq("attr_group_id",entity.getAttrGroupId()));
            if(relationList!=null&&relationList.size()!=0){
                List<Long> attrIds = relationList.stream().map((relationEntity)->{
                    return relationEntity.getAttrId();
                }).collect(Collectors.toList());
                List<AttrEntity> attrList = attrDao.selectBatchIds(attrIds);
                if(attrList!=null&&attrList.size()!=0){
                    attrGroupVo.setAttrs(attrList.stream().map((attrEntity)->{
                        AttrVo attrVo = new AttrVo();
                        BeanUtils.copyProperties(attrEntity,attrVo);
                        return attrVo;
                    }).collect(Collectors.toList()));
                }
            }
            return attrGroupVo;
        }).collect(Collectors.toList());
    }

    public List<SpuItemAttrGroup> getGroupAttrs1(Long spuId){
        List<ProductAttrValueEntity> paEntities = productAttrValueService.getBaseMapper().selectList(
                new QueryWrapper<ProductAttrValueEntity>().eq("spu_id", spuId));
        if (paEntities==null || paEntities.size()==0){
            return null;
        }
        List<Long> attrIds = paEntities.stream().map(e -> {
            return e.getAttrId();
        }).collect(Collectors.toList());
        List<AttrAttrgroupRelationEntity> relationEntityList = attrAttrgroupRelationService.getBaseMapper()
                .selectList(new QueryWrapper<AttrAttrgroupRelationEntity>().in("attr_id", attrIds));

        Map<Long,List<SpuBaseAttrVo>> map = new HashMap();
        for (AttrAttrgroupRelationEntity entity : relationEntityList) {
            Long groupId = entity.getAttrGroupId();
            List<SpuBaseAttrVo> attrVos  = map.get(entity.getAttrGroupId());
            if (attrVos==null){
                attrVos = new ArrayList<>();
                map.put(groupId,attrVos);
            }
            for (ProductAttrValueEntity paEntity : paEntities) {
                if (paEntity.getAttrId()==entity.getAttrId()){
                    SpuBaseAttrVo attrVo = new SpuBaseAttrVo();
                    attrVo.setAttrName(paEntity.getAttrName());
                    attrVo.setAttrValue(paEntity.getAttrValue());
                    attrVos.add(attrVo);
                }
            }
        }

        List<SpuItemAttrGroup> list = new ArrayList<>();
        for (Long groupId : map.keySet()) {
            SpuItemAttrGroup group = new SpuItemAttrGroup();
            group.setAttrs(map.get(groupId));
            AttrGroupEntity groupEntity = attrGroupService.getById(groupId);
            group.setGroupName(groupEntity.getAttrGroupName());
            list.add(group);
        }

        return list;
    }
    @Override
    public List<SpuItemAttrGroup> getGroupAttrs(Long catalogId, Long spuId) {
        return this.getBaseMapper().getGroupAttrs(catalogId,spuId);
    }
}